<template>
  <div>
    <div class="excelright" >

      <el-button type="primary" @click="submitFile">导入excel表
        
      </el-button>




      <!-- 导出excel文件 -->
      <el-button type="primary">
        <download-excel
          type="primary"
          class="export-excel-wrapper"
          :data="json_data"
          :fields="json_fields"
          name="filename.xls"
        >
          导出excel表
        </download-excel>
      </el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: '',
  props:{},
  data () {
    return {
           json_fields: {
        "工号": "jobnumber",    //常规字段
        "员工姓名（必填）": "name",   
        "手机号（必填）": "phone.mobile", //支持嵌套属性
        "部门": "department", //支持嵌套属性
        "职位": "post", //支持嵌套属性
        // "备用电话": {
        //   field: "phone.landline",
        //             //自定义回调函数
        //   callback: value => {
        //     return `Landline Phone - ${value}`;
        //   }
        // }
      },
      json_data: [
        {
          name: "魏佳",
          jobnumber: "2020001",
          post: "前端工程师",
          department: "开发部",
          phone: {
            mobile: "15701793476",
            landline: "13281357561"
          }
        },
      ],
      json_meta: [
        [
          {
            " key ": " charset ",
            " value ": " utf- 8 "
          }
        ]
      ]
    }
  },
  components: {},
  methods: {
    // handleSelectedFile (convertedData) {
    //   console.log(convertedData)
    // }
    submitFile() {
   const _this = this;
   if (!_this.files.name) {
    _this.$message.warning("请选择要上传的文件！");
    return false;
   }
   let fileFormData = new FormData();
   //filename是键，file是值，就是要传的文件
   fileFormData.append("file", _this.files, _this.files.name);
   if(_this.OtherParams){
    const keys=Object.keys(_this.OtherParams);
    keys.forEach(e=>{
     fileFormData.append(e, _this.OtherParams[e]);
    })
   }
   let requestConfig = {
    headers: {
     "Content-Type": "multipart/form-data"
    }
   };
   AjaxHelper.post(_this.apiURL, fileFormData, requestConfig)
    .then(res => {
     console.log(res);
     if (res.success) {
      const result = res.result;
      if (result.errorCount == 0 && result.successCount > 0) {
       _this.$message({
        message: `导入成功,成功${result.successCount}条`,
        type: "success"
       });
       _this.closeFileUpload();
       _this.Refresh();
      } else if (result.errorCount > 0 && result.successCount >= 0) {
       _this.Refresh();
       _this.tableData = result.uploadErrors;
       _this.successCount = result.successCount;
       _this.innerVisible = true;
      } else if (result.errorCount == 0 && result.successCount == 0) {
       _this.$message({
        message: `上传文件中数据为空`,
        type: "error"
       });
      }
     }
    })
    .catch(function(error) {
     console.log(error);
    });
  },
  },
  mounted() {},
  computed: {},
  watch: {}
}
</script>

<style lang='scss' scoped>
.excelright{
  margin-left: 1000px;
}
</style>